﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using WebDavProcessor.Interfaces;
using WebDavProcessor.Resources;

namespace WCFServer.DataProviders
{
    class FileSystemDataAdapter:DataAdapter
    {
        string root;
        public FileSystemDataAdapter(string directoryName)
        {
            root = directoryName;
        }
        
        public override void WriteResource(ResourceInfo resource)
        {
            FileStream fs = new FileStream(Path.Combine(root, resource.ResourceName), FileMode.OpenOrCreate);
            try
            {

                byte[] data = new byte[resource.Data.Length];
                resource.Data.Position = 0;
                resource.Data.Read(data, 0, data.Length);
                fs.Write(data, 0, data.Length);
                fs.Close();
                
            }
            catch(Exception)
            {
                try
                {
                    fs.Close();
                }
                catch
                {
                }
                throw;
            }
            
        }

        public override void DeleteResource(ResourceInfo resource)
        {
            try
            {
                File.Delete(Path.Combine(root, resource.ResourceName));

            }
            catch (Exception)
            {
                throw;
            }

        }

        public override void RenameResource(string oldResourceName, string newResourceName)
        {
            throw new NotImplementedException();
        }

        public override bool IsExist(string resourcePath)
        {
            FileInfo fi = new FileInfo(Path.Combine(root, resourcePath));
            return fi.Exists;
        }

        public override ResourceInfo ReadResource(string resourcePath, int offset, int count)
        {
            try
            {
                ResourceInfo res = ReadResourceLight(resourcePath);


                FileStream fs = File.OpenRead(Path.Combine(root, res.ResourceName));
                byte[] data = new byte[fs.Length];
                if (count < 0)
                    count = data.Length;
                fs.Read(data, offset, count);
                fs.Close();

                
                MemoryStream ms = new MemoryStream(data);
                res.Data = ms;
                return res;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public override ResourceInfo ReadResourceLight(string resourcePath)
        {
            try
            {
                ResourceInfo res = new ResourceInfo(resourcePath);

                FileInfo fi = new FileInfo(Path.Combine(root, res.ResourceName));
                res.Properties.Add("creationtime", fi.CreationTime.ToString());
                res.Properties.Add("length", fi.Length.ToString());
                res.Properties.Add("lastaccesstime", fi.LastAccessTime.ToString());
                res.Properties.Add("lastwritetime", fi.LastWriteTime.ToString());
                return res;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
